package com.strong.faker;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.RandomUtil;
import com.strong.Faker;
import com.strong.enum_.EmailDomainEnum;
import com.strong.enum_.MobilePhoneEnum;
import com.strong.enum_.ProvinceEnum;
import com.strong.model.IdNumber;
import com.strong.model.Person;
import com.strong.model.PersonName;
import com.strong.model.Place;
import lombok.extern.slf4j.Slf4j;

/**
 * 人 模拟数据生成类
 *
 * @author Administrator
 * @date 2023/04/08
 */
@Slf4j
public class PersonFaker extends BaseFaker<Person> {

    /**
     * 人名对象
     */
    private PersonName personName;

    /**
     * 身份证对象
     */
    private IdNumber idNumber;

    /**
     * 所在地对象
     */
    private Place place;

    /**
     * 电子邮件地址
     */
    private String strEmail;

    /**
     * 固话号码
     */
    private String strTelephone;

    /**
     * 手机号码
     */
    private String strMobilePhone;

    /**
     * 电子邮件域枚举
     */
    private EmailDomainEnum[] emailDomainEnums;

    /**
     * 手机号码枚举
     */
    private MobilePhoneEnum[] mobilePhoneEnums;

    /**
     * 省枚举 - 用于固定电话区号
     */
    private ProvinceEnum[] provinceEnums;

    /**
     * 注入 人名对象
     *
     * @param personName 人名对象
     * @return {@link PersonFaker }
     */
    public PersonFaker name(PersonName personName) {
        this.personName = personName;
        return this;
    }

    /**
     * 注入 身份证对象
     *
     * @param idNumber 身份证对象
     * @return {@link PersonFaker }
     */
    public PersonFaker idNumber(IdNumber idNumber) {
        this.idNumber = idNumber;
        return this;
    }

    /**
     * 注入 所在地对象
     *
     * @param place 所在地对象
     * @return {@link PersonFaker }
     */
    public PersonFaker place(Place place) {
        this.place = place;
        return this;
    }

    /**
     * 注入邮件域名
     *
     * @param emailDomainEnums 邮箱域名枚举数组
     * @return {@link EmailFaker}
     */
    public PersonFaker domain(EmailDomainEnum... emailDomainEnums) {
        this.emailDomainEnums = emailDomainEnums;
        return this;
    }

    /**
     * 注入号码段前缀
     *
     * @param mobilePhoneEnums 手机枚举数组
     * @return {@link MobilePhoneFaker}
     */
    public PersonFaker prefix(MobilePhoneEnum... mobilePhoneEnums) {
        this.mobilePhoneEnums = mobilePhoneEnums;
        return this;
    }

    /**
     * 注入省枚举
     *
     * @param provinceEnums 省枚举
     * @return {@link PlaceFaker}
     */
    public PersonFaker province(ProvinceEnum... provinceEnums) {
        this.provinceEnums = provinceEnums;
        return this;
    }

    @Override
    protected void initialize() {
        if (ObjUtil.isNull(this.personName)) {
            this.personName = Faker.personName().single();
        }
        if (ObjUtil.isNull(this.idNumber)) {
            this.idNumber = Faker.idNumber().gender(this.personName.getGenderEnum()).single();
        }
        if (ObjUtil.isNull(this.place)) {
            this.place = Faker.place().single();
        }

        // 注入邮件地址
        EmailFaker emailFaker = Faker.email();
        if (ObjUtil.isNotEmpty(this.emailDomainEnums)) {
            emailFaker.domain(this.emailDomainEnums);
        }
        this.strEmail = emailFaker.single();

        // 注入手机号码
        MobilePhoneFaker mobilePhoneFaker = Faker.mobilePhone();
        if (ObjUtil.isNotEmpty(this.mobilePhoneEnums)) {
            mobilePhoneFaker.prefix(this.mobilePhoneEnums);
        }
        this.strMobilePhone = mobilePhoneFaker.single();

        // 注入固话号码
        TelephoneFaker telephoneFaker = Faker.telephone();
        if (ObjUtil.isNotEmpty(this.provinceEnums)) {
            telephoneFaker.province(this.provinceEnums);
        }
        this.strTelephone = telephoneFaker.single();
    }

    @Override
    protected Person getModel() {
        return new Person(this.personName, this.idNumber, this.place, this.strEmail, this.strTelephone, this.strMobilePhone,
                RandomUtil.randomString(RandomUtil.randomInt(1000000, 99999999)));
    }
}
